package com.app.service.Impl;

import com.app.pojo.PaymentQuotaConfig;
import com.app.mapper.PaymentQuotaConfigMapper;
import com.app.service.IPaymentQuotaConfigService;
import com.app.service.ISysUserService;
import com.app.util.Result;
import com.app.vo.PaymentQuotaConfigVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 缴费额度表 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2024-07-29
 */
@Service
public class PaymentQuotaConfigServiceImpl extends ServiceImpl<PaymentQuotaConfigMapper, PaymentQuotaConfig> implements IPaymentQuotaConfigService {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private PaymentQuotaConfigMapper paymentQuotaConfigMapper;



    public void batchInsert(List<PaymentQuotaConfig> list) {
        this.saveBatch(list);
    }

    @Override
    public List<PaymentQuotaConfig> getPaymentQuotaConfigByQuota() {
        QueryWrapper<PaymentQuotaConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        return this.list(queryWrapper);
    }

    /**
     * Admin设置支付额度
     *
     * @param paymentQuotas
     * @return
     */
    @Override
    public void setPaymentQuota(List<BigDecimal> paymentQuotas,Integer userid) {
        paymentQuotaConfigMapper.delete(null);
        List<PaymentQuotaConfig> list = new ArrayList<>();
        for (int i = 0; i < paymentQuotas.size(); i++) {
            PaymentQuotaConfig paymentQuotaConfig = new PaymentQuotaConfig();
            paymentQuotaConfig.setQuota(paymentQuotas.get(i));
            paymentQuotaConfig.setCreateTime(LocalDateTime.now());
            paymentQuotaConfig.setCreateUser(userid);
            list.add(paymentQuotaConfig);
        }
        batchInsert(list);
    }

    /**
     * Admin支付金额的列表查询
     * @return
     */
    @Override
    public Result<List<PaymentQuotaConfig>> quotaList(Integer page, Integer limit) {
        Page<PaymentQuotaConfig> p =new Page(page,limit);
       IPage<PaymentQuotaConfig> list =  paymentQuotaConfigMapper.quotaList(p);
        PaymentQuotaConfigVo paymentQuotaConfigVo = new PaymentQuotaConfigVo();
        paymentQuotaConfigVo.setList(list.getRecords());
        paymentQuotaConfigVo.setCount((int) list.getTotal());
       return Result.success(paymentQuotaConfigVo);
    }

}
